/**
 * 
 */
package service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import utils.Page;
import dao.SecurityDao;
import dao.SecurityDaoImpl;
import dao.SeniorAdminDao;
import dao.SeniorAdminDaoImpl;
import exception.ApplicationException;

import model.Administrator;
import model.Door;
import model.Log;
import model.Message;
import model.User;

/**
 * @ClassName: SecurityServiceImpl
 * @Description:
 * @author Hongming Wu
 * @date 2013-12-6
 * 
 */
public class SecurityServiceImpl implements SecurityService {

	@Override
	public Door getDoorByID(String doorID) {
		Door door = null;

		try {
			SecurityDao sad = new SecurityDaoImpl();
			door = sad.getDoorByID(doorID);
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}
		return door;
	}

	@Override
	public void updateDoor(Door door, Administrator admin) {

		try {
			SecurityDao sd = new SecurityDaoImpl();
			SeniorAdminDao sad = new SeniorAdminDaoImpl();
			Log l = new Log();
			Administrator ad = new Administrator();
			ad = sad.getAdminByName(admin.getAdmin_Username());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date a = new Date();
			String str = df.format(a);
			l.setLog_Date(str);
			l.setOperator(ad.getAdmin_ID());
			l.setLog_Content(ad.getAdmin_ID() + "    " + door.getDoor_ID()
					+ " ----Open");
			sd.updateDoor(door);
			sd.createLog(l, door);
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}

	}

	@Override
	public void updateAll() {
		try {
			SecurityDao sad = new SecurityDaoImpl();
			sad.updateAll();
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}

	}

	@Override
	public Page loadPagedLogs(Page page, SecurityQueryHelper helper) {

		try {
			SecurityDao sad = new SecurityDaoImpl();
			page.setTotalRecNum(sad.count(helper));
			page.setPageContent(sad.getScopedLogs(helper, page.getStartIndex(),
					page.getEndIndex()));
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}
		return page;

	}

	@Override
	public List<Door> loadAllDoor() {
		List<Door> doorList = null;

		try {
			SecurityDao sd = new SecurityDaoImpl();
			doorList = sd.loadAllDoor();
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}
		return doorList;
	}

	@Override
	public Page loadPagedMsgs(Page page, SecurityQueryHelper helper) {
		try {
			SecurityDao sad = new SecurityDaoImpl();
			page.setTotalRecNum(sad.count(helper));
			page.setPageContent(sad.getScopedMsgs(helper, page.getStartIndex(),
					page.getEndIndex()));
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}
		return page;
	}

	@Override
	public Message getMsgByID(Integer msg_ID) {
		Message msg = null;

		try {
			SecurityDao sad = new SecurityDaoImpl();
			msg = sad.getMsgByID(msg_ID);
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}
		return msg;
	}

	@Override
	public void updateMsg(Message msg) {
		try {
			SecurityDao sad = new SecurityDaoImpl();
			sad.updateMsg(msg);
		} catch (Exception e) {
			throw new ApplicationException("error!", e);
		}
		
	}
}
